package com.mind.customer.handler.tongwei;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.mind.common.constant.CustomerConstants;
import com.mind.common.enums.DataType;
import com.mind.common.utils.StringUtils;
import com.mind.common.utils.file.vo.FileVo;
import com.mind.customer.api.ocr.OCRUtils;
import com.mind.customer.exception.AjObjectNotFoundException;
import com.mind.customer.handler.common.CommonHandler;
import com.mind.system.domain.AjObject;
import com.mind.system.domain.AjTicketTask;
import com.mind.system.domain.vo.Field;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Component
public class StampHandler {
    @Resource
    private CommonHandler commonHandler;
    private String stampKey = "SIGNATURE_STAMP";

    @Resource
    private OCRUtils ocrUtils;


    public JSONObject handler(String ticketType, String cate, String name, FileVo fileVo, AjTicketTask task) throws AjObjectNotFoundException, IOException {

        AjObject ajObject = commonHandler.getObject(ticketType,cate, name);
        List<Field> fields = JSONArray.parseArray(ajObject.getFields(), Field.class);
        if(isEnableStamp(fields)){
            JSONArray stampResult = handleStamp(ocrUtils.getStamp(fileVo, task));
            if(stampResult.size()>0){
                return new JSONObject(){{
                    put(stampKey, stampResult);
                }};
            }
        }
        return null;
    }

    public boolean isEnableStamp(List<Field> fields){
        for (Field field: fields){
            return Arrays.asList(field.getAttachmentDataField().split(",")).contains(stampKey) && DataType.ROW.getCode().equals(field.getDataType());
        }
        return false;
    }

    private JSONArray handleStamp(JSONObject stamp) {
        JSONArray stampResult = new JSONArray();
        if (stamp.containsKey("resultStamps")) {
            JSONArray stampArray = stamp.getJSONArray("resultStamps");
            for (int i = 0; i < stampArray.size(); i++) {
                JSONObject r = stampArray.getJSONObject(i);
                if(StringUtils.isNotEmpty("text") && StringUtils.isEmpty(r.getString("minor"))) {
                    r.put("minor", CustomerConstants.CORPORATE_SEAL);
                }
                r.put("resultSigFlag", stamp.getIntValue("resultSigFlag") != -1 ? CustomerConstants.YES : CustomerConstants.NO);
                stampResult.add(r);
            }
        }
        return stampResult;
    }
    /**
     * main方法
     */
     public static void main(String[] args){

         List<Field> fields = JSONArray.parseArray("[{\"id\":\"m8ii69xs.2447\",\"ruleDataField\":\"对象是否存在\",\"children\":[],\"ticketType\":\"\",\"attachmentDataField\":\"ObjectExist\",\"dataType\":\"header\",\"fieldType\":\"text\"},{\"id\":\"m8ii69xs.276\",\"attachmentDataField\":\"甲方\",\"ruleDataField\":\"甲方\",\"fieldType\":\"text\",\"dataType\":\"header\",\"styleConfig\":{\"filedId\":\"m8ii69xs.276\",\"isUse\":1,\"keyFontSize\":14,\"keyFontColor\":\"#333\",\"valueFontSize\":14,\"valueFontColor\":\"#333\"},\"children\":[],\"ticketType\":\"\"},{\"id\":\"m8ii6hzd.863\",\"attachmentDataField\":\"乙方\",\"ruleDataField\":\"乙方\",\"fieldType\":\"text\",\"dataType\":\"header\",\"styleConfig\":{\"filedId\":\"m8ii6hzd.863\",\"isUse\":1,\"keyFontSize\":14,\"keyFontColor\":\"#333\",\"valueFontSize\":14,\"valueFontColor\":\"#333\"},\"children\":[],\"ticketType\":\"\"},{\"id\":\"m8ii6i3t.2933\",\"attachmentDataField\":\"合同有效期\",\"ruleDataField\":\"合同有效期\",\"fieldType\":\"text\",\"dataType\":\"header\",\"styleConfig\":{\"filedId\":\"m8ii6i3t.2933\",\"isUse\":1,\"keyFontSize\":14,\"keyFontColor\":\"#333\",\"valueFontSize\":14,\"valueFontColor\":\"#333\"},\"children\":[],\"ticketType\":\"\"}]", Field.class);
         for (Field field: fields){
             System.out.println(field.toString());
             boolean flag = Arrays.asList(field.getAttachmentDataField().split(",")).contains("SIGNATURE_STAMP") && DataType.ROW.getCode().equals(field.getDataType());
             System.out.println(">>>>>>>>>"+flag);

         }
     }
}
